package com.ihd.ihardware.view.tzc.me.view;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.ihd.ihardware.R;
import com.ihd.ihardware.common.AppConstans;
import com.ihd.ihardware.databinding.ActivityDocumentBinding;
import com.ihd.ihardware.pojo.EmptyRes;
import com.ihd.ihardware.pojo.HealthRecordRes;
import com.ihd.ihardware.pojo.OPValue;
import com.ihd.ihardware.view.tzc.me.viewmodel.DocumentViewModel;

import org.jaaksi.pickerview.dataset.OptionDataSet;
import org.jaaksi.pickerview.picker.OptionPicker;
import org.jaaksi.pickerview.picker.TimePicker;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import cn.wowjoy.commonlibrary.base.BaseActivity;
import cn.wowjoy.commonlibrary.base.BaseConsumer;
import cn.wowjoy.commonlibrary.utils.ToastUtils;

public class DocumentActivity extends BaseActivity<ActivityDocumentBinding, DocumentViewModel> {

    private String freq;
    private String want;
    private String heal;
    private String id;
    private String sex;
    private String ysxg;

    private OptionPicker mSexPicker;
    private List<OPValue> sexs = new ArrayList<>();
    private TimePicker mTimePicker;
    private OptionPicker mHPicker;
    private List<OPValue> height = new ArrayList<>();
    private OptionPicker mWPicker;
    private List<OPValue> weight = new ArrayList<>();
    private OptionPicker mWaistPicker;
    private List<OPValue> waist = new ArrayList<>();
    private OptionPicker mWantPicker;
    private List<OPValue> wantList = new ArrayList<>();
    private OptionPicker mFreqPicker;
    private List<OPValue> freqList = new ArrayList<>();
    private OptionPicker mHealthPicker;
    private List<OPValue> healthList = new ArrayList<>();
    private OptionPicker mysxgPicker;
    private List<OPValue> ysxgList = new ArrayList<>();

    @Override
    protected void init(Bundle savedInstanceState) {
        mImmersionBar.statusBarDarkFont(true).init();
        binding.mtitlebar.setTitle("健康档案");
        binding.mtitlebar.setLeftImageResource(R.drawable.ic_back);
        binding.mtitlebar.setLeftClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

        sexs.add(new OPValue("男"));
        sexs.add(new OPValue("女"));
        for (float i = 30; i < 180; i++) {
            weight.add(new OPValue(String.valueOf(i)));
        }
        for (int i = 80; i < 251; i++) {
            height.add(new OPValue(String.valueOf(i)));
        }
        for (int i = 30; i < 150; i++) {
            waist.add(new OPValue(String.valueOf(i)));
        }
        wantList.add(new OPValue("健身"));
        wantList.add(new OPValue("减肥"));
        wantList.add(new OPValue("健美"));
        freqList.add(new OPValue("小于一次"));
        freqList.add(new OPValue("1-2次"));
        freqList.add(new OPValue("3次以上"));
        healthList.add(new OPValue("良好"));
        healthList.add(new OPValue("不宜剧烈运动"));
        ysxgList.add(new OPValue("饮食清淡"));
        ysxgList.add(new OPValue("爱吃零食"));
        ysxgList.add(new OPValue("饮食油腻"));
        ysxgList.add(new OPValue("经常喝酒"));

        try {
            mTimePicker = new TimePicker.Builder(this, TimePicker.TYPE_YEAR | TimePicker.TYPE_MONTH | TimePicker.TYPE_DAY, new TimePicker.OnTimeSelectListener() {
                @Override
                public void onTimeSelect(TimePicker picker, Date date) {
                    binding.bir.setText(new SimpleDateFormat("yyyy-MM-dd").format(date));
                }
            }).setRangDate(new SimpleDateFormat("yyyy-MM-dd").parse("1900-01-01").getTime(), System.currentTimeMillis()).create();
        } catch (ParseException e) {
        }

        mSexPicker = new OptionPicker.Builder(this, 1, new OptionPicker.OnOptionSelectListener() {
            @Override
            public void onOptionSelect(OptionPicker picker, int[] selectedPosition, OptionDataSet[] selectedOptions) {
                binding.sex.setText(sexs.get(selectedPosition[0]).getValue());
                sex = String.valueOf(selectedPosition[0] + 1);
            }
        }).create();

        mWPicker = new OptionPicker.Builder(this, 1, new OptionPicker.OnOptionSelectListener() {
            @Override
            public void onOptionSelect(OptionPicker picker, int[] selectedPosition, OptionDataSet[] selectedOptions) {
                binding.weight.setText(weight.get(selectedPosition[0]).getValue());
            }
        }).create();

        mHPicker = new OptionPicker.Builder(this, 1, new OptionPicker.OnOptionSelectListener() {
            @Override
            public void onOptionSelect(OptionPicker picker, int[] selectedPosition, OptionDataSet[] selectedOptions) {
                binding.height.setText(height.get(selectedPosition[0]).getValue());
            }
        }).create();

        mWaistPicker = new OptionPicker.Builder(this, 1, new OptionPicker.OnOptionSelectListener() {
            @Override
            public void onOptionSelect(OptionPicker picker, int[] selectedPosition, OptionDataSet[] selectedOptions) {
                binding.yaowei.setText(waist.get(selectedPosition[0]).getValue());
            }
        }).create();

        mWantPicker = new OptionPicker.Builder(this, 1, new OptionPicker.OnOptionSelectListener() {
            @Override
            public void onOptionSelect(OptionPicker picker, int[] selectedPosition, OptionDataSet[] selectedOptions) {
                binding.want.setText(wantList.get(selectedPosition[0]).getValue());
                want = String.valueOf(selectedPosition[0] + 1);
            }
        }).create();

        mFreqPicker = new OptionPicker.Builder(this, 1, new OptionPicker.OnOptionSelectListener() {
            @Override
            public void onOptionSelect(OptionPicker picker, int[] selectedPosition, OptionDataSet[] selectedOptions) {
                binding.active.setText(freqList.get(selectedPosition[0]).getValue());
                freq = String.valueOf(selectedPosition[0] + 1);
            }
        }).create();

        mHealthPicker = new OptionPicker.Builder(this, 1, new OptionPicker.OnOptionSelectListener() {
            @Override
            public void onOptionSelect(OptionPicker picker, int[] selectedPosition, OptionDataSet[] selectedOptions) {
                binding.health.setText(healthList.get(selectedPosition[0]).getValue());
                heal = String.valueOf(selectedPosition[0] + 1);
            }
        }).create();

        mysxgPicker = new OptionPicker.Builder(this, 1, new OptionPicker.OnOptionSelectListener() {
            @Override
            public void onOptionSelect(OptionPicker picker, int[] selectedPosition, OptionDataSet[] selectedOptions) {
                binding.ysxg.setText(ysxgList.get(selectedPosition[0]).getValue());
                ysxg = String.valueOf(selectedPosition[0] + 1);
            }
        }).create();

        setRx(AppConstans.HEALTH, new BaseConsumer<HealthRecordRes>() {
            @Override
            protected void onStart() {
            }

            @Override
            protected void onSuccess(HealthRecordRes healthRecordRes) {
                id = healthRecordRes.getData().getId();
                sex = healthRecordRes.getData().getSex();
                binding.sex.setText("1".equals(sex) ? "男" : "女");
                mSexPicker.setDataWithValues(sexs, "1".equals(sex) ? "男" : "女");
                binding.bir.setText(healthRecordRes.getData().getBirthday());
                binding.height.setText(healthRecordRes.getData().getHeight());
                binding.weight.setText(healthRecordRes.getData().getWeight());
                binding.yaowei.setText(healthRecordRes.getData().getWaist());
                want = healthRecordRes.getData().getWant();
                if ("1".equals(want))
                    binding.want.setText("健身");
                else if ("2".equals(want))
                    binding.want.setText("减肥");
                else if ("3".equals(want))
                    binding.want.setText("健美");
                freq = healthRecordRes.getData().getExerciseFrequency();
                if ("1".equals(freq))
                    binding.active.setText("小于一次");
                else if ("2".equals(freq))
                    binding.active.setText("1-2次");
                else if ("3".equals(freq))
                    binding.active.setText("3次以上");
                heal = healthRecordRes.getData().getHealth();
                if ("1".equals(heal))
                    binding.health.setText("良好");
                else if ("2".equals(heal))
                    binding.health.setText("不宜剧烈运动");
                ysxg = healthRecordRes.getData().getEatingHabits();
                if ("1".equals(ysxg))
                    binding.ysxg.setText("饮食清淡");
                else if ("2".equals(ysxg))
                    binding.ysxg.setText("爱吃零食");
                else if ("3".equals(ysxg))
                    binding.ysxg.setText("饮食油腻");
                else if ("4".equals(ysxg))
                    binding.ysxg.setText("经常喝酒");

                binding.birLL.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        try {
                            mTimePicker.setSelectedDate(new SimpleDateFormat("yyyy-MM-dd").parse(healthRecordRes.getData().getBirthday()).getTime());
                        } catch (ParseException e) {
                        }
                        mTimePicker.show();
                    }
                });

                mWPicker.setDataWithValues(weight, healthRecordRes.getData().getWeight());
                mHPicker.setDataWithValues(height, healthRecordRes.getData().getHeight());
                if (!TextUtils.isEmpty(healthRecordRes.getData().getWaist()))
                    mWaistPicker.setDataWithValues(waist, healthRecordRes.getData().getWaist().split("\\.")[0]);
                else
                    mWaistPicker.setDataWithValues(waist, "90");
                mWantPicker.setDataWithValues(wantList, "");
                mFreqPicker.setDataWithValues(freqList, "");
                mHealthPicker.setDataWithValues(healthList, "");
                mysxgPicker.setDataWithValues(ysxgList, "");
            }

            @Override
            protected void onError(Throwable error) {
                ToastUtils.showShort(DocumentActivity.this, error.getMessage());
            }
        });

        binding.sexLL.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mSexPicker.show();
            }
        });

        binding.heightLL.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mHPicker.show();
            }
        });

        binding.weightLL.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mWPicker.show();
            }
        });

        binding.yaoweiLL.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mWaistPicker.show();
            }
        });

        binding.wantLL.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mWantPicker.show();
            }
        });

        binding.activeLL.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mFreqPicker.show();
            }
        });

        binding.healthLL.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mHealthPicker.show();
            }
        });

        binding.ysxgLL.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mysxgPicker.show();
            }
        });

        binding.confirmTV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewModel.editHealthRecord(binding.bir.getText().toString(), freq, heal, binding.height.getText().toString(), TextUtils.isEmpty(id) ? "0" : id, sex, binding.yaowei.getText().toString(), want, binding.weight.getText().toString(), ysxg);
            }
        });

        setRx(AppConstans.EDITHEALTH, new BaseConsumer<EmptyRes>() {
            @Override
            protected void onStart() {
            }

            @Override
            protected void onSuccess(EmptyRes emptyRes) {
                finish();
            }

            @Override
            protected void onError(Throwable error) {
                ToastUtils.showShort(DocumentActivity.this, error.getMessage());
            }
        });

        viewModel.healthRecord();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_document;
    }

    @Override
    protected Class getViewModelClass() {
        return DocumentViewModel.class;
    }
}
